{{--/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/3/15 11:32
 * Desc:
 */--}}
@extends('layout.admin')

@section("title")
    分类管理
@endsection

@section('content')
    {{--修改样式--}}
    <link href="{{asset('css/table/table.css')}}" rel="stylesheet"/>
    <link href="{{asset('css/tree/tree.css')}}" rel="stylesheet" />
    <div class="layui-card-body ">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
                <li>@can("admin.category.create")
                        <a onclick="admin.openLayerForm('{{ route("admin.category.create",["groupId"=>$groupId]) }}', '添加分类', 'POST', '500px', 'auto',0,'#categroy_create')"
                           class="layui-btn" data-method="create" data-type="auto" id="create"><i class="layui-icon"></i>添加</a>
                    @endcan
                </li>
                @foreach($group as $g)
                    <li @if($groupId==$g["id"]) class="layui-this" @endif ><a href="{{route('admin.category.index',["groupId"=>$g["id"]])}}">{{$g["name"]}}</a></li>
                @endforeach

            </ul>
        </div>
    </div>


    @if($error>0)
        <div class="layui-card-body ">
            <table class="layui-table layui-form" lay-size="sm">
                <thead>
                <tr data-id="0">
                    <th>ID</th>
                    <th>排序</th>
                    <th>名称</th>
                    <th>父级ID</th>
                    <th>编码</th>
                    <th>描述</th>
                    <th style="text-align:center;">操作</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td colspan="7" style="text-align: center">没有数据</td>
                </tr>
                </tbody>
            </table>
        </div>
    @else
        <div class="layui-card-body ">
            <table class="layui-table layui-form" id="tree-table" lay-size="sm"></table>
        </div>
    @endif
@endsection

@section('script')

    <script src="{{asset('js/table/tree.js')}}"></script>
    <script>
        layui.use(['form', 'dropdown', 'util','table', 'treeTable'], function () {
            var table = layui.table,
                form = layui.form,
                treeTable = layui.treeTable,
                dropdown = layui.dropdown,
                util=layui.util;
            treeTable.render({
                elem: '#tree-table',
                data: {!! $categories !!},
                icon_key: 'name',
                parent_key: "parent_id",
                end: function (e) {
                    form.render();
                },
                cols: [
                    {
                        key: 'id',
                        title: 'ID',
                    },
                    {
                        key: 'sort',
                        title: '排序',
                        template: function (item) {
                            return "<div data-id="+item.id+" class='sort-blur' contenteditable=true>"+item.sort+"</div>"
                        }
                    },
                    {
                        key: 'name',
                        title: '名称',
                        template: function (item) {
                            if (item.parent_id == 0) {
                                return '<span style="color:red;">' + item.name + '</span>';
                            } else {
                                return '<span style="color:green;">' + item.name + '</span>';
                            }
                        }
                    },
                    {
                        key: 'parent_id',
                        title: '父级ID',
                    },
                    {
                        key: 'code',
                        title: '编码',
                        template: function (item) {
                            return item.code;
                        }
                    },
                    {
                        key: 'scope',
                        title: '适用范围',
                        template: function (item) {
                            let scope = item.scope_course+item.scope_exam
                            if(scope==2){
                                return "课程/考试";
                            }
                            if(scope==1){
                                return item.scope_course==1?"课程":"考试";
                            }
                            if(scope==0){
                                return "未分配";
                            }
                        }
                    },
                    {
                        key: 'description',
                        title: '描述',
                        template: function (item) {
                            return item.description;
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        template: function (item) {
                            return '<a class="layui-btn layui-btn-xs" lay-filter="buttons">管理<i class="layui-icon layui-icon-down"></i></a>';
                        }
                    }
                ]
            });

            treeTable.on('tree(buttons)',function(obj){
                var that = this,item = obj.item;
                //更多下拉菜单
                let buttons = [@if(_can("admin.category.show")){title: '查看详情', id: 'show',type:'link',link:"{{ route_uri("admin.category.show")}}",params:{id: item.id}},@endif]
                if(item.addStatus){
                    let temp =[
                            @if(_can("admin.category.create")){title: '添加分类', id: 'create',type:'openLayerForm',link:"{{ route_uri("admin.category.create")}}",params:{parent_id:item.id,groupId:'{{$groupId}}'},set:{title:"添加分类",method:"POST",width:"500px",height:"auto",repush:0,dome:"#categroy_create"}}, @endif
                    ]
                    buttons.push.apply(buttons,temp)
                }

                let temp2=[
                        @if(_can("admin.category.update")){title: '修改分类', id: 'edit',type:'openLayerForm',link:"{{ route_uri("admin.category.update") }}",params:{id:item.id},set:{title:"修改分类",method:"POST",width:"500px",height:"auto",repush:0,dome:"#categroy_edit"}}, @endif
                        @if(_can("admin.category.destroy")){title: '删除分类', id: 'del',type:'tableDataDelete',link:"{{ route_uri("admin.category.destroy") }}",params:{id:item.id},set:{dome:this,title:"是否删除该分类"}}, @endif
                        @if(_can("admin.category.update.attach")){title: '标签设置', id: 'assign',type:'openLayerForm',link:"{{ route_uri("admin.category.update.attach") }}",params:{id:item.id},set:{title:"《"+item.name+"》设置标签",method:"POST",width:"450px",height:"380px",repush:0,dome:"#categroy_tag"}} @endif
                ]
                buttons.push.apply(buttons,temp2)
                if(item.scope_exam) {
                    let temp3 = [ @if(_can("admin.category.update.attach.1")){title: '设置版本', id: 'version',type:'link',link:"{{ route_uri("admin.category.update.attach.1")}}",params:{id: item.id}}@endif]
                    buttons.push.apply(buttons,temp3)
                }
                dropdown.render(tableButtonGroup(that, buttons))
            })

        });
    </script>
    <script src="{{asset('js/layCommon.js')}}"></script>
@endsection

